﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CreateWeldPoint.CUiCreateWpCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Reflection;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.CreateWeldPoint;

[TxPowerEngineeringCommandCategory]
public class CUiCreateWpCmd : TxToggleButtonCommand
{
  private CApCreateWpEnabler m_createWPEnabler;
  private CUiCreateWpTCForm _createWpForm;
  private ITxObject _prevWeldContainer;
  private ITxObject _prevWeldOperation;
  private bool _unpressing;

  private bool Pressed { set; get; }

  public CUiCreateWpCmd()
  {
    this.Pressed = false;
    this._unpressing = false;
    this.m_createWPEnabler = new CApCreateWpEnabler();
  }

  public virtual bool Push => this.Pressed;

  public virtual void Execute(object cmdParams)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      if (!TxTcUtilities.IsTcAnyPlatform)
        return;
      if (!this.Pressed)
      {
        this._createWpForm = new CUiCreateWpTCForm();
        this.InitTCFormWithLastUsedObjects();
        ((Form) this._createWpForm).FormClosed += new FormClosedEventHandler(this._createWpForm_FormClosed);
        ((Control) this._createWpForm).Show();
      }
      else
        this.FinalizeCreateWeldPointsMode();
    }
    finally
    {
      Cursor.Current = current;
    }
  }

  private void CommandsManager_CommandExecuting(
    object sender,
    TxCommandsManager_CommandExecutingEventArgs args)
  {
    if (args.CommandID == ((object) this).ToString())
    {
      this._unpressing = true;
    }
    else
    {
      if (this._unpressing || !(args.CommandID != "GraphicViewerModes.ModeCreateWeldPoint.1") || !(args.CommandID != ((object) this).ToString()))
        return;
      this.StopCreateWeldPointsMode();
    }
  }

  private void _createWpForm_FormClosed(object sender, FormClosedEventArgs e)
  {
    if (((Form) this._createWpForm).DialogResult == DialogResult.Yes)
    {
      this.Pressed = true;
      TxApplication.CommandsManager.CommandExecuting += new TxCommandsManager_CommandExecutingEventHandler(this.CommandsManager_CommandExecuting);
      if (!CApCreateWpTCLogic.IsWpConfiguredAsPrivate())
        this._prevWeldContainer = this._createWpForm.txebWeldContainer.Object;
      this._prevWeldOperation = this._createWpForm.txebWeldOperation.Object;
    }
    this._createWpForm = (CUiCreateWpTCForm) null;
  }

  public virtual string Category => this.GetResourceManager().GetString("WELD_CMD_CATEGORY");

  public virtual string Name => this.GetResourceManager().GetString("CREATE_WELD_POINTS_CMD_NAME");

  private ResourceManager GetResourceManager()
  {
    Assembly assembly = ((object) this).GetType().Assembly;
    return new ResourceManager(assembly.GetName().Name + ".StringTable", assembly);
  }

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("CreateWeldPointByPick");

  public virtual string Bitmap => this.GetResourceManager().GetString("CREATE_WELD_POINTS_CMD_BMP");

  public virtual string Description
  {
    get => this.GetResourceManager().GetString("CREATE_WELD_POINTS_CMD_DESCRIPTION");
  }

  public virtual string[] AdvancedDescriptions => TxCallMessages.CollectMessages((ITxCommand) this);

  public virtual string Tooltip
  {
    get => this.GetResourceManager().GetString("CREATE_WELD_POINTS_CMD_TOOLTIP");
  }

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_createWPEnabler;

  public virtual bool Licensed => TxCommandLicenser.CreateWeldPoints;

  private void StopCreateWeldPointsMode()
  {
    try
    {
      new CApCreateWpTCLogic().RunCreateWeldPointsMode((ITxObject) null, (ITxObject) null);
    }
    finally
    {
      this.FinalizeCreateWeldPointsMode();
    }
  }

  private void FinalizeCreateWeldPointsMode()
  {
    this.Pressed = false;
    this._unpressing = false;
    TxApplication.CommandsManager.CommandExecuting -= new TxCommandsManager_CommandExecutingEventHandler(this.CommandsManager_CommandExecuting);
  }

  private void InitTCFormWithLastUsedObjects()
  {
    if (!CApCreateWpTCLogic.IsWpConfiguredAsPrivate() && this._prevWeldContainer != null && this._prevWeldContainer.IsValid())
      this._createWpForm.txebWeldContainer.Object = this._prevWeldContainer;
    if (this._prevWeldOperation == null || !this._prevWeldOperation.IsValid())
      return;
    this._createWpForm.txebWeldOperation.Object = this._prevWeldOperation;
  }
}
